package com.msl.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.msl.MslApp;
import com.msl.R;
import com.msl.base.BaseFragment;
import com.msl.bean.AbBussBean;
import com.msl.bean.AbDcodeBean;
import com.msl.bean.AbStoreBean;
import com.msl.dialog.SelectDefineDialog;
import com.msl.tool.ToastUtils;
import java.io.IOException;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;
import study.com.wheelviewlibrary.listener.SelectInterface;

public class FragmentSCRK2 extends BaseFragment implements SelectInterface {
String tag = "FragmentSCRK2";
    protected View mView = null;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tvc1)
    TextView tvc1;
    @Bind(R.id.tvc2)
    TextView tvc2;
    @Bind(R.id.tvc3)
    TextView tvc3;
    @Bind(R.id.rl1)
    RelativeLayout rl1;
    @Bind(R.id.rl2)
    RelativeLayout rl2;
    @Bind(R.id.rl3)
    RelativeLayout rl3;

    private AbDcodeBean abDcodeBean = new AbDcodeBean();

    private String res1="";
    private AbStoreBean abs;
    private String[] res1s ;
    private int index1=0; ;
    SelectDefineDialog defineDialog1;

    private String res2="";
    private AbBussBean abb;
    SelectDefineDialog defineDialog2;
    private int index2=0 ;
    private String[] res2s ;

    SelectDefineDialog defineDialog3;
    private int index3=0 ;
    private String[] res3s ;
//    public static String id1="";
//    public static String id2="";
//    public static String id3="";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragmentscrk2, null);
        ButterKnife.bind(this, mView);
        abDcodeBean = (AbDcodeBean) getActivity().getIntent().getSerializableExtra(MslApp.codedea);
        tv1.setText("发货单号:"+abDcodeBean.getData().get(0).getRelationBillCode());
        tv2.setText("收货单位:"+abDcodeBean.getData().get(0).getDesStoreName());
        waitDialog.show();

        getbase(MslApp.url+"?action=DownloadBaseInfo&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                +"&baseType="+"6"
                ,0
        );

        getbase(MslApp.url+"?action=DownDanJuZhuanHuanInfo&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                        +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                        +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                        +"&billType="+"116"
                        + "&billcode=" + abDcodeBean.getData().get(0).getBillcode()
                ,1
        );
        return mView;
    }

    void getbase(String url,int type)
    {
        final  int tt= type;
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.isSuccessful()){//回调的方法执行在子线程。
                        if(tt==0)
                        {
                            res1 = response.body().string();
                            Log.e(tag,res1);
                        }
                        else
                        {
                            res2 = response.body().string();
                            Log.e(tag,res2);
                        }
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(tt);
                    }
                    else
                    {
                        Log.e(tag,"获取数据失败了"+response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }





    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        MslApp.id1="";
        MslApp.id2="";
        MslApp.id3="";
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @OnClick({R.id.rl1, R.id.rl2, R.id.tv1, R.id.rl3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl1:
                defineDialog2=null;
                defineDialog3=null;
                defineDialog1 = new SelectDefineDialog(getActivity(), res1s, this);
                defineDialog1.showDialog();
                break;
            case R.id.rl2:
                defineDialog1=null;
                defineDialog3=null;
                defineDialog2 = new SelectDefineDialog(getActivity(), res2s, this);
                defineDialog2.showDialog();
                break;
            case R.id.rl3:
                defineDialog1=null;
                defineDialog2=null;
                defineDialog3 = new SelectDefineDialog(getActivity(), res3s, this);
                defineDialog3.showDialog();
                break;

        }
    }

    @Override
    public void selectedResult(String result) {

        if (defineDialog1 != null) {
            for (int i = 0; i < res1s.length; i++) {
                if (res1s[i].equals(result)) {
                    index1 = i;
                    tvc1.setText(result);
                    MslApp.id1= abs.getData().get(index1).getCode();
                    break;
                }
            }
        }
        if (defineDialog2 != null) {
            for (int i = 0; i < res2s.length; i++) {
                if (res2s[i].equals(result)) {
                    index2 = i;
                    tvc2.setText(result);
                    MslApp.id2= abb.getData().get(index2).getBillChangeTypeCode();
                    break;
                }
            }
        }
        if (defineDialog3 != null) {
            for (int i = 0; i < res3s.length; i++) {
                if (res3s[i].equals(result)) {
                    index3 = i;
                    tvc3.setText(result);
                    MslApp.id3= abb.getData().get(index3).getTransactionCode();
                    break;
                }
            }
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    abs = gson.fromJson(res1, AbStoreBean.class);
                    if (abs.getErrcode() == 0 && abs.getData().size()!=0) {
                        res1s = new String[abs.getData().size()];
                        for (int i = 0; i < abs.getData().size(); i++) {
                            res1s[i] = abs.getData().get(i).getName();
                        }
                        rl1.setVisibility(View.VISIBLE);
                        tvc1.setText(res1s[0]);
                        MslApp.id1= abs.getData().get(0).getCode();
                        if(abDcodeBean.getData().get(0).getStoreInfo().length()!=0)
                        {
                            for (int i = 0; i <abs.getData().size() ; i++) {
                                if(abs.getData().get(i).getCode().equals(abDcodeBean.getData().get(0).getStoreInfo()))
                                {
                                    index1 = i;
                                    tvc1.setText(res1s[i]);
                                    MslApp.id1= abs.getData().get(i).getCode();
                                    break;
                                }
                            }

                        }
                    }
                    else
                    {
                        ToastUtils.showSingleToast(getActivity(),"基础信息获取失败,请重新下载");
                    }
                    break;
                case 1:
                    abb = gson.fromJson(res2, AbBussBean.class);
                    if (abb.getErrcode() == 0 && abb.getData().size()!=0) {
                        res2s = new String[abb.getData().size()];
                        res3s = new String[abb.getData().size()];
                        for (int i = 0; i < abb.getData().size(); i++) {
                            res2s[i] = abb.getData().get(i).getBillChangeTypeName();
                            res3s[i] = abb.getData().get(i).getTransactionName();
                        }
                        rl2.setVisibility(View.VISIBLE);
                        rl3.setVisibility(View.VISIBLE);


                        tvc2.setText(res2s[0]);
                        tvc3.setText(res3s[0]);
                        MslApp.id2= abb.getData().get(index2).getBillChangeTypeCode();
                        MslApp.id3= abb.getData().get(index3).getTransactionCode();
                        if(abDcodeBean.getData().get(0).getTransactionCode().length()!=0)
                        {
                            for (int i = 0; i <abb.getData().size() ; i++) {
                                if(abb.getData().get(i).getTransactionCode().equals(abDcodeBean.getData().get(0).getTransactionCode()))
                                {
                                    index2 = i;
                                    index3 = i;
                                    tvc2.setText(res2s[i]);
                                    tvc3.setText(res3s[i]);
                                    MslApp.id2= abb.getData().get(i).getBillChangeTypeCode();
                                    MslApp.id3= abb.getData().get(i).getTransactionCode();
                                    break;
                                }
                            }

                        }
                    }
                    else
                    {
                        ToastUtils.showSingleToast(getActivity(),"基础信息获取失败,请重新下载");
                    }
                    break;
                default:
                    break;
            }
        }
    };


}
